package com.ycfmedical.bed.present;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bigkoo.pickerview.TimePickerView;
import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.ControlConstants;
import com.ycfmedical.bed.local.DataManager;
import com.ycfmedical.bed.local.entity.TimerControl;
import com.ycfmedical.bed.ui.fragment.TimingControlFragment;
import com.ycfmedical.bed.utils.AlarmManagerUtil;
import com.ycfmedical.bed.utils.ShareUtil;

import android.content.Context;
import android.widget.TextView;
import android.widget.Toast;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import rx.Observable;

/**
 * Created by Administrator on 2017/3/20 0020.
 */

public class TimerControlPresenter extends XPresent<TimingControlFragment>
{
    /**
     * 日期矫正 dayNow为当月天数 dayStart为起始日期，dayNext为终止日期,dayValue为终止日期与当月天数的差值， monthStart为起始月份，monthNext为终止月份 hourStart为起始小时
     * minuteStart为起始分钟数
     */
    private TextView date_tv;
    private TimePickerView pvTime;
    Context context;
    private Observable<List<TimerControl>> listObservable;
    private String time = "";

    public TimerControlPresenter(Context context)
    {
        this.context = context;
    }

    // 进入页面后首先加载用户数据

    public void loadData()
    {
        Object timerControls = ShareUtil.getInstance().getObject(Constants.CONTROL_TIMING,TimerControl.class);
        TimerControl timingControl = (TimerControl)timerControls;
        List<TimerControl> timingControlList = new ArrayList<>();
        timingControlList.add(timingControl);
        showData(timingControlList);
    }

    // 展示数据
    public void showData(List<TimerControl> timerControls)
    {
        int size = timerControls.size();
        getV().getAdapter().setData(timerControls);
    }

    public void TimePicker(int timingId)
    {
        pvTime = new TimePickerView(context, TimePickerView.Type.HOURS_MINS);
        pvTime.setTime(new Date());
        pvTime.setCyclic(false);
        pvTime.setCancelable(true);
        // 时间选择后回调
        pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener()
        {
            @Override
            public void onTimeSelect(Date date)
            {
                time = getTime(date);
                //在缓存中中添加时间
                addShareData(timingId,time);

            }

        });
        // 设置时间
        timeShow();
        setTiming(timingId);
    }

    public static String getTime(Date date)
    {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(date);
    }

    public void timeShow()
    {
        pvTime.show();
    }


    // 设置定时任务
    private void setTiming(int postId)
    {
        if (time != null && time.length() > 0)
        {
            String[] times = time.split(":");
            AlarmManagerUtil.setAlarm(context,
                1,
                Integer.parseInt(times[0]),
                Integer.parseInt(times[1]),
                0,
                0,
                "提醒时间到",
                2);
            Toast.makeText(context, "定时任务设置成功", Toast.LENGTH_LONG).show();
        }
    }

    // 在使用sharepreference存取数据中添加定时任务
    private void addShareData(int postId, String time)
    {
        TimerControl timingBean = new TimerControl();
        timingBean.setId(postId);
        timingBean.setTimes(time);
        Map<String,TimerControl> TimingMap = new HashMap<>();
        TimingMap.put(Constants.CONTROL_TIMING,timingBean);
        ShareUtil.getInstance().setObject(Constants.CONTROL_TIMING,TimingMap);
    }

    // 删除使用sharepreference存取数据中设置的定时数据数据
    public void deleteShareData(String times)
    {
        ShareUtil.getInstance().deleteConfig(Constants.CONTROL_TIMING);
    }

}
